package forums.service;

import java.util.List;

import javax.annotation.security.RolesAllowed;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import forums.model.Forum;
import forums.model.Message;

/*
 * Author       :       Willie Wheeler and John Wheeler
 * Book         :       'Spring In Practice' [Book written by Willie Wheeler and John Wheeler]
 */
@Component
public class ForumFilter {
	
	@Autowired
	private MessageListFilter msgListFilter;
	
	public void setMessageListFilter(MessageListFilter filter) {
		this.msgListFilter = filter;
	}
	
	/**
	 * <p>
	 * Returns a copy of the passed forum, with unauthorized messages filtered
	 * out.
	 * </p>
	 * 
	 * @param origForum
	 * @return copy of original filter, with unauthorized messages filtered out
	 */
	@RolesAllowed({ "IS_AUTHENTICATED_ANONYMOUSLY", "AFTER_ACL_READ" })
	public Forum filter(Forum origForum) {
	    Forum copyForum = new Forum(origForum.getId(), origForum.getName());
	    List<Message> copyMsgs = msgListFilter.filter(origForum.getMessages());
	    copyForum.setMessages(copyMsgs);
	    return copyForum;
	}
}
